
//初始化编辑器
window.UEDITOR_CONFIG = {
    // 为编辑器实例添加一个路径，这个不能被注释
    UEDITOR_HOME_URL: basePath+"js/ueditor/",
    // 服务器统一请求接口路径
    serverUrl:basePath+ "ueditor/dispatch"
}
var ue = UE.getEditor('serviceContent', {
    initialFrameHeight:350,//编辑区域高度
    maximumWords:1024, autoHeightEnabled: false,
    autoFloatEnabled: true,allowDivTransToP: false,
    toolbars: [[
        'preview', //预览
        'source', //源代码
        'bold', //加粗
        'indent', //首行缩进
        'italic', //斜体meta
        'underline', //下划线
        'strikethrough', //删除线
        'subscript', //下标
        'fontborder', //字符边框
        'superscript', //上标
        'formatmatch', //格式刷
        'blockquote', //引用
        'pasteplain', //纯文本粘贴模式
        'horizontal', //分隔线
        'removeformat', //清除格式
        'time', //时间
        'date', //日期
        'link',
        'unlink', //取消链接
        'inserttitle', //插入标题
        // 'insertcode', //代码语言
        'fontfamily', //字体
        'fontsize', //字号
        // 'paragraph', //段落格式
        // 'emotion', //表情
        'spechars', //特殊字符
        'simpleupload', //单图上传
        'justifyleft', //居左对齐
        'justifyright', //居右对齐
        'justifycenter', //居中对齐
        'justifyjustify', //两端对齐
        'forecolor', //字体颜色
        'backcolor', //背景色
        // 'insertorderedlist', //有序列表
        // 'insertunorderedlist', //无序列表
        // 'directionalityltr', //从左向右输入
        // 'directionalityrtl', //从右向左输入
        'rowspacingtop', //段前距
        'rowspacingbottom', //段后距
        'imagenone', //默认
        'imageleft', //左浮动
        'imageright', //右浮动
        'imagecenter', //居中
        'lineheight', //行间距
        'edittip ', //编辑提示
        'customstyle', //自定义标题
        'autotypeset', //自动排版
        // 'help', //帮助
        'cleardoc', //清空文档
    ]]
});

var selectedRow = dotJson;
selectedRow.appType == null?selectedRow.appType = '':selectedRow.appType;
selectedRow.hasMidPage == null?selectedRow.hasMidPage = '':selectedRow.hasMidPage;
selectedRow.discountPrice == null?selectedRow.discountPrice = '':selectedRow.discountPrice;
// 根据有没有时间填写 折扣时间的必填动作
if(selectedRow.discountPrice != null){
    $("#discountStartTime").prop("required",true);
    $("#discountEndTime").prop("required",true);
}

// 因为vue是双向绑定和js的数据浅拷贝的问题 这边用到JSON.parse-JSON.stringify 做数据的深拷贝
var VueObj = new Vue({
    el:"#vue",
    data:{addBizApp:selectedAppRow,editBizApp:JSON.parse(JSON.stringify(selectedRow))}
});
function changeDiscountPrice(e){
    if($(e).val().length > 0){
        $("#discountStartTime").prop("required",true);
        $("#discountEndTime").prop("required",true);
    }else{
        $("#discountStartTime").removeProp("required");
        $("#discountEndTime").removeProp("required");
    }
}
$(function () {
    laydate.render({
        elem: '#discountStartTime'
        ,type: 'datetime'
    });

    laydate.render({
        elem: '#discountEndTime'
        ,type: 'datetime'
    });
});

ue.ready(function() {
    ue.body.innerHTML = selectedRow.serviceContent?selectedRow.serviceContent:"";
});




function onchangeh5(e) {
    if($(e).val() == 'html'){
        $("#h5").removeAttrs("hidden");
    }else{
        $("#h5").attr("hidden",'hidden');
    }
}


function doEditBizApp() {
    if(selectedRow == null){
        toastr.error(common_no_choose_msg.format("操作"));
        return;
    }
    if(!$("#editBizAppform").valid()){
        return ;
    }
    // if($.trim(ue.getContent()).length == 0){
    //     toastr.error("服务说明为必填");
    //     return;
    // }
    if($("#appType").val() == 'html' && $.trim($("#appLinkUrl").val()) == ''){
        toastr.error("h5地址为必填");
        return;
    }
    var premium = $.trim($("#premium").val());
    if(premium == ''){
        toastr.error("溢价为必填");
        return;
    }
    // 可以为0
    var reg = /^(0|[1-9][0-9]{0,2})(\.[0-9]{1,2})?$/;
    // 不能为0
    var reg1 = /^0(\.[0-9]{1,2})|^([1-9][0-9]{0,2})(\.[0-9]{1,2})?$/;
    if(!reg1.test(premium)){
        toastr.error("溢价为0.xx~999.99的两位小数");
        return;
    }

    var originalPrice = $.trim($("#originalPrice").val());
    if(originalPrice != '' && !reg1.test(originalPrice)){
        toastr.error("原价为0.xx~999.99的两位小数");
        return;
    }
    var discountPrice = $.trim($("#discountPrice").val());
    if(discountPrice != '' && !reg.test(discountPrice)){
        toastr.error("折扣价为0~999.99的两位小数");
        return;
    }

    console.log(premium);
    console.log(originalPrice);
    console.log(discountPrice);
    console.log(originalPrice >= premium);
    console.log(discountPrice >= premium);
    // 三个价格的比较
    // 溢价 premium > 原价 originalPrice
    // 溢价 premium > 折扣价 discountPrice
    if(originalPrice != '' && premium != '' && (parseFloat(originalPrice) >= parseFloat(premium))){
        toastr.error("原价不能大于等于溢价");
        return;
    }

    if( discountPrice != ''  && premium != '' && (parseFloat(discountPrice) >= parseFloat(premium))){
        toastr.error("折扣价不能大于等于溢价");
        return;
    }
    var postData = {};
    $("#editBizAppform").serializeArray().map((function (item,index,input) {
        postData[item.name] = item.value;
    }));
    if(discountPrice.length > 0 &&  new Date($("#discountStartTime").val()).getTime() >= new Date($("#discountEndTime").val()).getTime()){
        toastr.error("开始时间不能大于结束时间");
        return;
    }
    postData.serviceContent = $.trim(ue.getContent());
    postData.id = selectedRow.id;
    postData.appid = selectedRow.appid;
    postData.isvPid = selectedRow.isvPid;
    postData.bizCode = selectedRow.bizCode;
    postData.orgCode = selectedRow.orgCode;
    postData.refCode = selectedRow.refCode;
    postData.appCategory = selectedRow.appCategory;
    postData.status =$("#status").val();
    postData.validDay = $.trim($("#validDay").val());
    postData.sortNum = $.trim($("#sortNum").val());
    if(discountPrice.length > 0 ){
        postData.discountStartTime = $("#discountStartTime").val();
        postData.discountEndTime = $("#discountEndTime").val();
    }else{
        postData.discountStartTime = "";
        postData.discountEndTime = "";
    }
    console.log(postData);
    $.ajax({
        type: 'post',
        url: basePath + 'bizApp/editBizAppChild',
        dataType: 'json',
        contentType: "application/json",
        beforeSend:function () {
            showLoading();
        },
        complete:function(){
            hideLoading();
        },
        data: JSON.stringify(postData),
        success : function (data) {
            if(data.result){
                toastr.success("服务项编辑成功");
                doBack2();
            }else{
                toastr.error(data.message);
            }
        },
        error: function (request) {
            toastr.error(common_system_error);
        }
    });
}

function doBack() {
    var postData = {};
    $("#editBizAppform").serializeArray().map((function (item,index,input) {
        postData[item.name] = item.value;
    }));
    postData.serviceContent = $.trim(ue.getContent());
    postData.id = selectedRow.id;
    postData.appid = selectedRow.appid;
    postData.isvPid = selectedRow.isvPid;
    postData.bizCode = selectedRow.bizCode;
    postData.orgCode = selectedRow.orgCode;
    postData.refCode = selectedRow.refCode;
    postData.appCategory = selectedRow.appCategory;
    postData.status =$("#status").val();
    postData.validDay = $.trim($("#validDay").val());
    postData.sortNum = $.trim($("#sortNum").val());
    postData.discountStartTime = $("#discountStartTime").val();
    postData.discountEndTime = $("#discountEndTime").val();
    postData.discountStartTime = $("#discountStartTime").val();
    postData.discountEndTime = $("#discountEndTime").val();
    console.log(!Compare(postData,selectedRow));
    if(Compare(postData,selectedRow)){
        doBack2();
    }else{
        swalClosePageToastr(doBack2);
        return;
    }
}


function doBack2() {
      console.log(searchStrAfter(searchStr2));
        window.location.href = basePath + 'bizApp/bizDocChildrenBiz?t=' + new Date().getTime()+"&appid="+appid+"&searchStr="+searchStr+searchStrAfter(searchStr2);
}
